package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/18 7:38
 * description:
 */
@Service
public class DtsCommentServiceImpl implements DtsCommentService {

    @Autowired
    DtsCommentMapper commentMapper;


    @Override
    public Map<String,Object> querySelective(
            List<Integer> brandIds, String userId, String valueId,
            Integer page, Integer limit, String sort, String order) {
        IPage<DtsComment> ipage = new Page<>(page, limit);

        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();
        if(userId != null){
            wrapper.eq("user_id", userId);
        }
        if(brandIds != null && brandIds.size() > 0){
            wrapper.in("g.grand_id", brandIds);
        }

        if(valueId != null){
            wrapper.eq("value_id", valueId);
        }

        if(sort != null && order!= null){
            if(order.equalsIgnoreCase("asc")){
                wrapper.orderByAsc(sort);
            }else{
                wrapper.orderByDesc(sort);
            }
        }

        ipage = commentMapper.selectGoodsAndComment(ipage, wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return data;
    }

    @Override
    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }
}
